﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _3
{
    public partial class Form1 : Form
    {
        int a, b;
        string Ope;
        int score;
        int result;
        Random r = new Random();
        double timeLeft = 10; // 倒计时初始为10秒

        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1000; // 设置每1秒触发一次
            /*timer1.Tick += new EventHandler(timer1_Tick); // 绑定事件处理程序*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                warn.Visible = true;
                warn.BackColor = Color.White;
                warn.Text = "请作答";
                return;
            }

            string str = textBox1.Text;
            textBox1.Text = ""; // 清除原内容
            double d = double.Parse(str);

            timer1.Stop(); // 用户答题时停止计时

            if (result == d)
            {
                score++;
                warn.Visible = true;
                warn.BackColor = Color.Green;
                warn.Text = "答对啦";
            }
            else
            {
                warn.Visible = true;
                warn.BackColor = Color.Red;
                warn.Text = "答错啦";
            }

            point.Text = score.ToString();
            GenerateNewQuestion(); // 生成新题目
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            GenerateNewQuestion(); // 生成新题目
        }

        private void GenerateNewQuestion()
        {
            // 生成随机题目
            a = r.Next(100) + 1;
            b = r.Next(100) + 1;
            int c = r.Next(4);

            switch (c)
            {
                case 0: Ope = "+"; result = a + b; break;
                case 1: Ope = "-"; result = a - b; break;
                case 2: Ope = "*"; result = a * b; break;
                case 3: Ope = "/"; result = a / b; break;
            }

            // 更新题目信息
            n1.Text = a.ToString();
            n2.Text = b.ToString();
            op.Text = Ope;
            textBox1.Text = "";

            // 重置倒计时
            timeLeft = 10;
            timerLabel.Text = timeLeft + " 秒"; // 显示剩余时间
            timer1.Start(); // 启动计时器
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft--;// 每次Tick事件减少1秒
                timerLabel.Text = timeLeft + " 秒"; // 更新显示
            }
            else
            {
                timer1.Stop(); // 时间到，停止计时器
                warn.Visible = true;
                warn.BackColor = Color.Red;
                warn.Text = "时间到";

                GenerateNewQuestion(); // 切换到下一题
            }
        }


        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void n1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void point_Click(object sender, EventArgs e)
        {

        }


        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void warn_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click_1(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
